export function html2Text (val) {
  const div = document.createElement('div')
  div.innerHTML = val
  return div.textContent || div.textContent
}

export function debounce (func, wait, immediate) {
  let timeout, args, context, timestamp, result

  const later = function () {
    // 据上一次触发时间间隔
    const last = +new Date() - timestamp

    // 上次被包装函数被调用时间间隔last小于设定时间间隔wait
    if (last < wait && last > 0) {
      timeout = setTimeout(later, wait - last)
    } else {
      timeout = null
      // 如果设定为immediate===true，因为开始边界已经调用过了此处无需调用
      if (!immediate) {
        result = func.apply(context, args)
        if (!timeout) { context = args = null }
      }
    }
  }

  return function (...args) {
    context = this
    timestamp = +new Date()
    const callNow = immediate && !timeout
    // 如果延时不存在，重新设定延时
    if (!timeout) { timeout = setTimeout(later, wait) }
    if (callNow) {
      result = func.apply(context, args)
      context = args = null
    }

    return result
  }
}

export function uniqueArr (arr) {
  return Array.from(new Set(arr))
}

export function saoYiSaoMessage (params = '') {
  if (window.webkit !== undefined) {
    window.webkit.messageHandlers.saoyisao.postMessage(params)
  } else if (window.Send !== undefined) { window.Send.saoyisao() }
}

// 打电话
export function callPhone (phone) {
  if (window.webkit !== undefined) {
    window.webkit.messageHandlers.callup.postMessage(phone)
  } else if (window.Send !== undefined) { window.Send.callup(phone) }
}

export function share (params = '') {
  if (window.webkit !== undefined) {
    window.webkit.messageHandlers.share.postMessage(params)
  } else if (window.Send !== undefined) {
    window.Send.share(params)
  }
}

export function sharezixun (params = '') {
  if (window.webkit !== undefined) {
    window.webkit.messageHandlers.sharezixun.postMessage(params)
  } else if (window.Send !== undefined) {
    window.Send.sharezixun(params)
  }
}

export function sharejilu (params = '') {
  if (window.webkit !== undefined) {
    window.webkit.messageHandlers.sharejilu.postMessage(params)
  } else if (window.Send !== undefined) {
    window.Send.sharejilu(params)
  }
}

export function storeValue (params = '') {
  if (window.webkit !== undefined) {
    window.webkit.messageHandlers.storeValue.postMessage(params)
  } else if (window.Send !== undefined) {
    window.Send.storeValue(params)
  }
}

export function getAppValue (params = '') {
  if (window.webkit !== undefined) {
    window.webkit.messageHandlers.getValue.postMessage(params)
  } else if (window.Send !== undefined) {
    window.Send.getValue(params)
  }
}

export function faceIdCard (params = '') {
  if (window.webkit !== undefined) {
    window.webkit.messageHandlers.faceIdCard.postMessage(params)
  } else if (window.Send !== undefined) { window.Send.faceIdCard(params) }
}

export function faceRecognize (params = '') {
  if (window.webkit !== undefined) {
    window.webkit.messageHandlers.faceRecognize.postMessage(params)
  } else if (window.Send !== undefined) { window.Send.faceRecognize(params) }
}

export function loginIn (params = '') {
  if (window.webkit !== undefined) {
    window.webkit.messageHandlers.loginIn.postMessage(params)
  } else if (window.Send !== undefined) { window.Send.loginIn(params) }
}

export function setStatusBar (params = '') {
  if (window.webkit !== undefined) {
    window.webkit.messageHandlers.setStatusBar.postMessage(params)
  } else if (window.Send !== undefined) { window.Send.setStatusBar(params) }
}

export function closePin (params = '') {
  if (window.webkit !== undefined) {
    window.webkit.messageHandlers.closePin.postMessage(params)
  } else if (window.Send !== undefined) { window.Send.closePin(params) }
}
